/****************************/
/* directory of Census data */
/* Original */
libname tbrp "P:\Donnees\TbRP\";
libname tbrpmin "P:\Donnees\TbRP\mini";
/* Sortie */
libname harmo "P:\Donnees\RP_Harmo\"; /* taille longue */
libname hmini "P:\Donnees\RP_Harmo\mini"; /* petite taille */
/*************************************/
/* directory of correspondence table */
libname ccom "P:\Donnees\Code_commune";
libname statdes "P:\Resultats\StatDesRP";
libname ilo9099 "P:\Donnees\iloiris9099\SAS";

/**************************************************************************/
/* rajouter les salaires par profession pour chaque RP !!! from FQP, LFS  */
/**************************************************************************/
%macro popiris;
/* calcule la population par IRIS */
proc freq data=sample noprint;
weight sond;
table iris / out = popiris; 
run;
data popiris;
set popiris;
drop percent;
rename count = popiris;
run;
proc sort data=sample;
by iris;
run;
data sample;
merge sample popiris;
by iris;
elim=(popiris<800);
run;
%mend popiris;

%macro rapidSD(year);
/* statdes */
%statdes(pnaid,&year);
%statdes(pnaic,&year);
%statdes(immig,&year);
%statdes(europe,&year);
%statdes(neurope,&year);
%statdes(actif,&year);
%statdes(chom,&year);
%statdes(csp,&year);
%statdes(hlm,&year);
%statdes(moco,&year);
%statdes(coupch,&year);
%statdes(monop,&year);
%statdes(hhd,&year);
%statdes(indus,&year);
%mend rapidSD;

%macro statdesf6812(var);
data f68;
set statdes.&var.68;
rename count = p68 percent = f68;
label count = p68 percent = f68;
run;
data f75;
set statdes.&var.75;
rename count = p75 percent = f75;
label count = p75 percent = f75;
run;
data f82;
set statdes.&var.82;
rename count = p82 percent = f82;
label count = p82 percent = f82;
run;
data f90;
set statdes.&var.90;
rename count = p90 percent = f90;
label count = p90 percent = f90;
run;
data f99;
set statdes.&var.99;
rename count = p99 percent = f99;
label count = p99 percent = f99;
run;
data f07;
set statdes.&var.07;
rename count = p07 percent = f07;
label count = p07 percent = f07;
run;
data f12;
set statdes.&var.12;
rename count = p12 percent = f12;
label count = p12 percent = f12;
run;
data statdes.F_&var;
merge f68 f75 f82 f90 f99 f07 f12;
by &var;
run;
proc datasets library=statdes;
delete &var.68 &var.75 &var.82 &var.90 &var.99 &var.07 &var.12; 
run;
quit;
%mend statdesf6812;
/*****************/
/* Macro StatDes */
/*****************/
%macro statdes(var,year);
Proc freq data=sample noprint ;
weight sond;
table &var / out=statdes.&var.&year;
run;
%mend statdes;


%macro corrUU99(year);
/******************/
/* Macro corrUU99 */
/******************/
/* correspondence UU99 */

/***************************************/
/* Ajuster pour PARIS LYON & MARSEILLE */
title "&year";

/* Premi�re �tape: remplace le CC du RP avec avec le CC2012 */
/* merge avec cg12 */
proc sort data=sample;
by depcom;
run;
data cg12;
set ccom.cg12;
run;
proc sort data=cg12 nodupkeys;
by depcom;
run;

/* correction Paris, Lyon, Marseille */
data sample;
merge sample (in = x) cg12;
by depcom;
if x;
/* correction car cc pour quartiers dans les RP68 & 75 pour Paris
et non cc commune */
if missing(cg12) & d="75" then cg12="75056";
run;

data sample;
set sample;
mcg12=missing(cg12);
run;
title "&year part de mauvaise fusion avec cc12 ";
proc freq data=sample;
table mcg12;
run;
/* Deuxi�me �tape: introduit 
/* merge avec UU99 */
proc sort data=sample;
by cg12;
run;
data sample;
merge sample (in = x) ccom.uu99;
by cg12;
if x;
run;
data sample;
set sample;
muu99=missing(uu99);
run;

proc freq data=sample;
table muu99;
run;
title "&year part de mauvaise fusion avec uu90";
data problem;
set sample;
if not(missing(uu99)) then delete;
run;
title "&year communes non fusionn�es avec uu90";
proc freq data=problem;
table depcom;
run;
/********************************************/
/* zones rurales et missing, alloue "DDXXX" */
data sample;
set sample;
if missing(uu99) then uu99=cats(d,"XXX");
run;
%mend;


%macro hcent(year);
/*****************************************/
/* create homogenous census tracts: MACRO */
/*****************************************/
/********************************************/
/* construction of homogenous census tracts */
/********************************************/
/* calcule la taille des quartiers existants */
proc freq data=sample noprint;
weight sond;
table quartier / out=quartier;
run;
/* try to reduce the size of big "quartier" */
/* rep�re les 'gros' quartiers + 6200 hbts */
data quartier;
set quartier;
big=(count>6200);
keep quartier big;
run;

proc sort data=sample;
by quartier;
run;

proc sort data=sample;
by quartier;
run;

data sample;
merge sample quartier;
by quartier;
run;

/* Si gros quartier alors utilise Tract */
data sample;
set sample;
if big=0 then nquartier=quartier;
/* if big quartier then we use the tract */
if big=1 then nquartier=tract;
drop big;
run;

proc freq data=sample noprint;
weight sond;
table nquartier / out=nquartier;
run;

data nquartier;
set nquartier;
big2=(count>6200);
keep nquartier big2;
run;


proc sort data=sample;
by nquartier;
run;

data sample;
merge sample nquartier;
by nquartier;
run;

data sample;
set sample;
/* if big tract then we use the ilot */
if big2=1 then nquartier=ilot;
run;

/****************************************************/
/* Au niveau ilot, on va agr�ger les ilots continus */
/****************************************************/

/*** before aggregation *******/
proc freq data=sample noprint;
weight sond;
table nquartier / out=nquartier1;
run;

/********************************/
/* agregate small neighborhoods */
/********************************/
proc freq data=sample noprint;
weight sond;
table nquartier / out=nquartier;
run;

data nquartier;
set nquartier;
city=substr(nquartier,1,5);
run;
/* agr�ge les ilots jusqu'� d�passer 3000 */
data nquartier2;
set nquartier;
by city;
if first.city then totp=0;
totp+count;
/*if count>1200 then totp=0;*/
if totp>3000 then totp=0;
run;

data nquartier2;
set nquartier2;
by city;
if first.city then nnumb=0;
if totp=0 then nnumb+1;
run;

data nquartier2;
set nquartier2;
if totp=0 then delete;
nquartierbis=compress(city!!"x"!!nnumb);
keep nquartier nquartierbis;
run;

proc sort data=sample;
by nquartier;
run;

data sample;
merge sample nquartier2;
by nquartier;
if not(missing(nquartierbis)) then nquartier=nquartierbis;
drop nquartierbis;
run;

/*********************/
/* after aggregation */
proc freq data=sample noprint;
weight sond;
table nquartier / out=nquartier2;
run;
/***********************************************/

proc freq data=sample noprint;
weight sond;
table nquartier / out=nquartier;
run;

data nquartier;
set nquartier;
keep count nquartier;
rename count=popquartier;
run;

proc sort data=sample;
by nquartier;
run;
data sample;
merge sample nquartier;
by nquartier;
run;
/**********************************************/
data sample;
set sample;
elim=(popquartier<800);
popiris=popquartier;
run;
title " &year : census tract too small eliminated ";
proc freq data=sample;
weight sond;
table elim / out=elim;
run;
%mend hcent;

/********/
/* 1968 */
/********/
data sample;
set tbrp.rp68;
run;

data sample;
length ilot $14;
length tract $14;
length quartier $14;
length cgeo $5;
set sample;
/*************************/
ilot=cats(d,c,qu,il);
/*************************/
tract=substr(ilot,1,9);
quartier=cats(d,c,qu);
/****************************/
/* create municipality code */
cgeo=cats(d,c);
run;

/*
RP 1968
Cr�ation d'une variable
� partir des 5 variables NIM, LCM, PCF, LCF et NEF
/* doit corriger pour un petit nombre d'individus d�clar�s comme 
ayant une famille mais sans donn�es sur la famille */

data sample;
     set sample;
     length moco $2;
     select;
         when (lcm='0')               moco='40';
         when (nim='1')               moco='32';
         when (lcf='0')               moco='31';
         when (lcf='4') select;
             when (pcf='1')           moco='11';
             when (pcf='0')           moco='12';
			 when (identfam='')   moco="11";
 
             end;
         when (lcf in ('1','2')) select;
             when (pcf='1') select;
                 when (nef='0')       moco='21';
                 when ('1'<=nef<='9') moco='22';
                 end;
             when (pcf='0')           moco='23';
			 when (identfam='')   moco="21";		 
             end;
		 when (identfam='')   moco="40";
         end;
run;

data sample;
set sample;
length pnaic $7;
length pnaid $15;
length constd $9;

/* sexe */
if s=1 then homme=1;
else homme=0;
drop s;
/* age */
age = input(ad,3.0); 
drop ad;

/* couple */
if moco='21' or moco='22' then coup=1;
else coup=0;
/* couple with children */
if moco='22' then coupch=1;
else coupch=0;
/* famille monoparentale */
monop=(moco='23');
/* Head of Household */
hhd=(lcm='1');

/* immigrant status */
if IN=2 or IN=3 then immig=1;
else immig=0;

/* people born in France are not considered immigrants */
if pn='**' or pn='91' or pn='92' or pn='93' or pn='94' or pn='95' or pn='96' or pn='97' or pn='98' then immig=0; 

/* living place in 1962 */
if cm1=6 then abd=1;
else abd=0;
/* new immigrants */
if immig=1 and abd=1 then nimmig=1;
else nimmig=0;

/* europe & neuropee */
/* use the country of birth */
if not(pn='**') then pn2=input(pn,best4.);
/* use the country of birth */
europe=(pn2=>1 and pn2<=29)*immig;
neurope=immig*(1-europe);

/* r�gions harmonis�es PNAIC */
/* origins */
/* europe */
if pn2=>1 and pn2<=29 then pnaic='Europe';
/* dom tom */
/*if pn2=>91 and pn2<=98 then pnai="Europe";*/
/* Africa */
if pn2=>30 and pn2<=59 then pnaic="Africa";
/* Asia */
if pn2=>71 and pn2<=85 then pnaic="Asieor";
if (pn="74" or pn="75" or pn="78" or pn="79" or pn="80" or pn="84") then 
pnaic="Asiemo";
/* Other */
/* Amerique */
if pn2=>60 and pn2<=69 then pnaic="Other";
/* Oceanie */
if pn2=>86 and pn2<=89 then pnaic="Other";
/* Unknown */
if pn2=99 then pnaic="Other";
/* Countries */
if pn2=30 then pnaic="Maghreb";
if pn2=45 then pnaic="Maghreb";
if pn2=52 then pnaic="Maghreb";
if immig=0 then pnaic="Natif";

/* pays harmonis�s PNAID */

if pn='01'	 then pnaid="allemagne";
if pn='02'	 then pnaid="autriche";
if pn='03'	 then pnaid="belgique";
if pn='04'	 then pnaid="bulgarie";
if pn='71' then pnaid="cambodge";
if pn='05'	 then pnaid="danemark";
if pn='06'	 then pnaid="espagne";
if pn='07'	 then pnaid="finlande";
if pn='08'	 then pnaid="grece";
if pn='09'	 then pnaid="hongrie";
if pn='10'	 then pnaid="irlande";
if pn='11'	 then pnaid="italie";
if pn='12'	 then pnaid="luxembourg";
if pn='13'	 then pnaid="norvege";
if pn='14'	 then pnaid="pays-bas";
if pn='15' then pnaid="pologne";
if pn='16'	 then pnaid="portugal";
if pn='16' then pnaid="portugal";
if pn='17'	 then pnaid="roumanie";
if pn='18'	 then pnaid="uk";
if pn='19'	 then pnaid="suede";
if pn='20'	 then pnaid="suisse";
if pn='21'	 then pnaid="tchecoslovaquie";
if pn='22'	 then pnaid="yougoslavie";
if pn='31' or pn="30" then pnaid="algerie";
if pn='30'	then pnaid='algerie';
if pn='33'	 then pnaid="cameroun";
if pn='34'	 then pnaid="centrafrique";
if pn='35'	 then pnaid="congo";
if pn='36'	 then pnaid="cotedivoire";
if pn='37'	 then pnaid="dahomey";
if pn='39'	 then pnaid="gabon";
if pn='40'	 then pnaid="guinee";
if pn='42'	 then pnaid="haute-volta";
if pn='43'	 then pnaid="madagascar";
if pn='44'	 then pnaid="mali";
if pn='45' then pnaid="maroc";
if pn='46'	 then pnaid="mauritanie";
if pn='47'	 then pnaid="niger";
if pn='48'	 then pnaid="egypte";
if pn='49'	 then pnaid="senegal";
if pn='50'	 then pnaid="tchad";
if pn='51'	 then pnaid="togo";
if pn='71'	 then pnaid="cambodge";
if pn='72'	 then pnaid="chine";
if pn='73'	 then pnaid="inde";
if pn='74'	 then pnaid="iran";
if pn='75'	 then pnaid="israel";
if pn='76'	 then pnaid="japon";
if pn='77'	 then pnaid="laos";
if pn='78'	 then pnaid="liban";
if pn='79'	 then pnaid="syrie";
if pn='80'	 then pnaid="turquie";
if pn='52' then pnaid="tunisie";
if pn='81'	 then pnaid="vietnam";
if pn='85'	 then pnaid="urss";
/********************************/
if immig=0 then pnaid="Natif";
if immig=1 and missing(pnaid) then pnaid=pnaic;

/* Education : DIP */
/* 4 groupes: {CEPP,BEPC,etc...},{Niveau Bac:brevet pro,etc},{Bac},{Bac+}*/
if DIP='10' or DIP='11' then educ='CEPP';
if DIP='20' or DIP='21' or DIP='22' or DIP='23' or DIP='30' or DIP='31' then educ='BEPC';
if DIP='40' or DIP='41' or DIP='42' or DIP='43' or DIP='44' or DIP='45' then educ='NBAC';
if DIP='50' then educ='BACP';
/* LF statut */
/* actifs et non �tudiant */
if TA=1 then emploi=1;
else emploi=0;
if TA=1 or TA=3 then actif=1;
else actif=0;
if TA=5 or TA=2 then etud=1;
else etud=0;
if TA=6 then milit=1;
else milit=0;
if TA=3 then chom=1;
else chom=0;
drop ta;
/***************************/
/* CSP: Cad, Ouv, Employ�s */
/***************************/
length csp $3;
if (csd='30' |csd='32' | csd='33' | csd='34') then csp="cad";
if (csd='51' | csd='53') then csp="emp";
if (csd='60' | csd='61' | csd='63' | csd='65' | csd='66'| csd='67' | csd='68') then csp="ouv";

/* indus */
indus=(cae3='2' | cae3='4');

/* surpeuplement */
/* mod�r� & accentu� */
surpm=(ip='4' | ip='5');
/* accentu� */
surp=(ip='5');

/* profession d�taill�e */
prof = pr;

/* movers */
movers=1-(cm1="1");

/* construction date CONSTD */
if aac3="1" then constd="0b1871";
if aac3="2" then constd="1871-1914";
if aac3="3" then constd="1915-1948";
if aac3="4" then constd="1949-1953";
if aac3="5" then constd="1954-1961";
if aac3="6" then constd="1962-1968";

/* age of the building BUILDAG */
if aac3="1" then buildag=97;
if aac3="2" then buildag=76;
if aac3="3" then buildag=37;
if aac3="4" then buildag=17;
if aac3="5" then buildag=11;
if aac3="6" then buildag=3;

run;

/* statdes */

/* Correction Zones Locales */
%corrUU99(1968);

%hcent(1968);

data sample;
length iris $16;
set sample;
/* D�finitions G�ographiques */
ville=uu99;
iris=ville!!nquartier;
run;

Data harmo.hrp68;
set sample (keep = cgeo age actif chom coup coupch elim educ etud 
europe hhd homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof surp surpm movers
constd buildag );
run;

Data hmini.hrp68;
set sample (keep = cgeo age actif chom coup coupch elim educ etud 
europe hhd homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof surp surpm movers 
constd buildag );
y=ranbin(3,1,0.01);
if y=0 then delete;
drop y;
sond=sond*100;
run;

/********/
/* 1975 */
/********/
data sample;
length depcom $5;
set tbrp.rp75;
depcom=cats(d,c);
run;

data sample;
length ilot $14;
length tract $14;
length quartier $14;
length cgeo $5;
set sample;
/*************************/
ilot=cats(d,c,qu,il);
/*************************/
tract=substr(ilot,1,9);
quartier=cats(d,c,qu);
/****************************/
/* create municipality code */
cgeo=cats(d,c);
run;

/**************************************************************
Programme de cr�ation d'une variable moco transmis par A Kych *
du CMH 
Cr�ation d'une variable
� partir des 5 variables NIM, LCM, PCF, LCF et NEF
*/
data sample;
     set sample;
     length moco $ 2;
     select;
         when (lcm='0')               moco='40';
         when (nim='1')               moco='32';
         when (lcf='0')               moco='31';
         when (lcf='4') select;
             when (pcf='1')           moco='11';
             when (pcf='0')           moco='12';
             end;
         when (lcf in ('1','2')) select;
             when (pcf='1') select;
                 when (nef='0')       moco='21';
                 when ('1'<=nef<='9') moco='22';
                 end;
             when (pcf='0')           moco='23';
             end;
         end;
     run;

data sample;
set sample;
length pnaic $7;
length pnaid $15;
length constd $9;

/* sexe */
if s=1 then homme=1;
else homme=0;
drop s;

/* age */
age=AD*1;
drop ad;

/* couple */
if moco='21' or moco='22' then coup=1;
else coup=0;
/* couple with children */
if moco='22' then coupch=1;
else coupch=0;
/* famille monoparentale */
monop=(moco='23');
/* Head of Household */
hhd=(lcm='1');

/* immigrant status */
if  NC='2' | NC='3' | NC='4' | NC='5' | NC='6' | NC='7' then immig=1;
else immig=0;
/* people born in France are not considered immigrant */
if pn='**' or pn='91' or pn='92' or pn='93' or pn='94' or pn='95' or pn='96' or pn='97' or pn='98' then immig=0; 

/* living abroad in 1968 */
if cm1=7 then abd=1;
else abd=0;
drop cm1;
/* new immigrants */
if immig=1 and abd=1 then nimmig=1;
else nimmig=0;

/* use the country of birth */
if not(pn='**') then pn2=input(pn,best4.);
/* origins */
/* europe */
if pn2=>0 and pn2<=29 then pnaic='Europe';
/* Africa */
if pn2=>31 and pn2<=59 then pnaic="Africa";
/* Asia */
if pn2=>71 and pn2<=85 then pnaic="Asieor";
if (pn="74" or pn="75" or pn="78" or pn="79" or pn="80" or pn="84") then 
pnaic="Asiemo";
/* Other */
/* Amerique */
if pn2=>60 and pn2<=69 then pnaic="Other";
/* Oceanie */
if pn2=>86 and pn2<=89 then pnaic="Other";
/* */
if pn2=31 then pnaic="Maghreb";
if pn2=45 then pnaic="Maghreb";
if pn2=52 then pnaic="Maghreb";
/*************************/
if immig=0 then pnaic="Natif";
neurope=immig*(1-(pn2=>1 and pn2<=29));
/* europe */
europe=(pn2=>0 and pn2<=29)*immig;

/* pays harmonis�s PNAID */
/* Pnaid */
length pnaid $ 15;
if pn='01'	 then pnaid="allemagne";
if pn='02'	 then pnaid="autriche";
if pn='03'	 then pnaid="belgique";
if pn='04'	 then pnaid="bulgarie";
if pn='05'	 then pnaid="danemark";
if pn='06'	 then pnaid="espagne";
if pn='07'	 then pnaid="finlande";
if pn='08'	 then pnaid="grece";
if pn='09'	 then pnaid="hongrie";
if pn='10'	 then pnaid="irlande";
if pn='11'	 then pnaid="italie";
if pn='12'	 then pnaid="luxembourg";
if pn='13'	 then pnaid="norvege";
if pn='14'	 then pnaid="pays-bas";
if pn='15'	 then pnaid="pologne";
if pn='16'	 then pnaid="portugal";
if pn='17'	 then pnaid="roumanie";
if pn='18'	 then pnaid="uk";
if pn='19'	 then pnaid="suede";
if pn='20'	 then pnaid="suisse";
if pn='21'	 then pnaid="tchecoslovaquie";
if pn='22'	 then pnaid="yougoslavie";
if pn='31' then pnaid="algerie";
if pn='33'	 then pnaid="cameroun";
if pn='34'	 then pnaid="centrafrique";
if pn='35'	 then pnaid="congo";
if pn='36'	 then pnaid="cotedivoire";
if pn='37'	 then pnaid="dahomey";
if pn='39'	 then pnaid="gabon";
if pn='40'	 then pnaid="guinee";
if pn='42'	 then pnaid="haute-volta";
if pn='43'	 then pnaid="madagascar";
if pn='44'	 then pnaid="mali";
if pn='45' then pnaid="maroc";
if pn='46'	 then pnaid="mauritanie";
if pn='47'	 then pnaid="niger";
if pn='48'	 then pnaid="egypte";
if pn='49'	 then pnaid="senegal";
if pn='50'	 then pnaid="tchad";
if pn='51'	 then pnaid="togo";
if pn='52' then pnaid="tunisie";
if pn='71'	 then pnaid="cambodge";
if pn='72'	 then pnaid="chine";
if pn='73'	 then pnaid="inde";
if pn='74'	 then pnaid="iran";
if pn='75'	 then pnaid="israel";
if pn='76'	 then pnaid="japon";
if pn='77'	 then pnaid="laos";
if pn='78'	 then pnaid="liban";
if pn='79'	 then pnaid="syrie";
if pn='80'	 then pnaid="turquie";
if pn='81'	 then pnaid="vietnam";
if pn='85'	 then pnaid="urss";
if missing(pnaid) then pnaid=pnaic;
if immig=0 then pnaid="Natif";
if immig=1 and missing(pnaid) then pnaid=pnaic;

/* education */
/* 4 groupes: {CEPP,BEPC,etc...},{Niveau Bac:brevet pro,etc},{Bac},{Bac+}*/
if DIP='10' or DIP='11' then educ='CEPP';
if DIP='20' or DIP='30' or DIP='31' then educ='BEPC';
if DIP='40' or DIP='41' or DIP='42' or DIP='43' or DIP='44' or DIP='45' then educ='NBAC';
if DIP='50' or DIP='51' or DIP='52' or DIP='53' or DIP='54' or DIP='55' then educ='BACP';
drop dip;

/* LF statut */
/* actifs et non �tudiant */
if TA=1 then emploi=1;
else emploi=0;
if TA=1 or TA=3 then actif=1;
else actif=0;
if TA=5 then etud=1;
else etud=0;
if TA=6 then milit=1;
else milit=0;
if TA=3 then chom=1;
else chom=0;
drop ta;

/* CSP */
if (csd='30' |csd='32' | csd='33' | csd='34') then csp="cad" ;
if (csd='51' | csd='53') then csp="emp" ;
if (csd='60' | csd='61' | csd='63' | csd='65' | csd='66'|
csd='67' | csd='68') then csp="ouv" ;

/* industrial classification 
ae100=substr(ae,1,2);
if not(ae100='**') then ae100b=input(ae100,best4.);
/* Industrie *
if not(missing(ae100b)) then indus=(AE100b<=56);*/

indus=(sect14='02' | sect14='03' | sect14='04' | sect14='05' | sect14='06');

/* profession d�taill�e */
prof = pr;

/* surpeuplement */
/* mod�r� & accentu� */
surpm=(ip='4' | ip='5');
/* accentu� */
surp=(ip='5');

/* movers */
movers=1-(ira="1");

/* construction date CONSTD */
if aac1="1" then constd="0b1871";
if aac1="2" then constd="1871-1914";
if aac1="3" then constd="1915-1948";
if aac1="4" then constd="1949-1961";
if aac1="5" then constd="1962-1967";
if aac1="6" then constd="1968-1975";

/* age of the building BUILDAG */
if aac1="1" then buildag=104;
if aac1="2" then buildag=83;
if aac1="3" then buildag=44;
if aac1="4" then buildag=20;
if aac1="5" then buildag=11;
if aac1="6" then buildag=4;

run;
title '1975';

/* Correction Zones Locales */
%corrUU99(1975);


%hcent(1975);

data sample;
length iris $16;
set sample;
/* d�finitions g�ographiques */
ville=uu99;
iris=ville!!nquartier;
run;

Data harmo.hrp75;
set sample (keep = cgeo age actif chom coup coupch elim educ etud 
europe hhd homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof surp surpm movers
constd buildag );
run;

Data hmini.hrp75;
set sample (keep = cgeo age actif chom coup coupch elim educ etud 
europe hhd homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof surp surpm movers
constd buildag );
/*******************/
y=ranbin(3,1,0.01);
if y=0 then delete;
drop y;
sond=sond*100;
/******************/
run;

/********/
/* 1982 */
/********/

data sample;
length depcom $5;
set tbrp.rp82;
depcom=cats(d,c);
cgeo=depcom;
run;

data sample;
length ilot $14;
length quartier $14;
set sample;
/*************************/
ilot=cats(d,c,cil,il);
/*************************/
quartier=cats(d,c,cil);
tract=quartier;
run;

data sample;
set sample;
/* MOCO */
if moco='23' or moco='33' then moco2='11';
if moco='43' or moco='44' then moco2='12';
if moco='01' or moco='02' or moco='11' or moco='12' then moco2='21';
if moco='21' or moco='22' or moco='31' or moco='32' then moco2='22';
if moco='41' or moco='42' then moco2='23';
if moco='53' or moco='54' or moco='55' or moco='56' or moco='57' or moco='58' or moco='59' then moco2='31';
if moco='61' or moco='62' or moco='63' or moco='64' or moco='65' or moco='66' or moco='67' or moco='68' or moco='69' then moco2='31';
if moco='60' then moco2='32';
if missing(moco2) then moco2='40';
drop moco;
rename moco2 = moco;
run;

data sample;
set sample;
length pnaic $7;
length pnaid $15;
length constd $9;

/* 1982 /

/* hlm */
hlm=(sod=22);

/* sexe */
if s=1 then homme=1;
else homme=0;
drop s;
/* age */
age = input(ad,3.0); 

/* couple */
if moco='21' or moco='22' then coup=1;
else coup=0;
/* couple with children */
if moco='22' then coupch=1;
else coupch=0;
/* famille monoparentale */
monop=(moco='23');
/* Head of Household */
hhd=(lprm='1');

/* immigrant status */
if  IN='12' | IN='20' then immig=1;
else immig=0;
drop in;
/* people born in France are not considered immigrant */
if pn='**' or pn='91' or pn='92' or pn='93' or pn='94' or pn='95' or pn='96' or pn='97' or pn='98' then immig=0; 

/* living abroad in 1975 */
if cm2="8" then abd=1;
else abd=0;
drop cm2;

/* new immigrants */
if immig=1 and abd=1 then nimmig=1;
else nimmig=0;

/* europe & neuropee */
if not(pn='**') then pn2=input(pn,best4.);
europe=(pn2=>0 and pn2<=29)*immig;
neurope=immig*(1-europe);

/* r�gions harmonis�es PNAIC */
/* europe */
if pn2=>0 and pn2<=29 then pnaic='Europe';
/* Africa */
if pn2=>31 and pn2<=59 then pnaic="Africa";
/* Asia */
if pn2=>71 and pn2<=85 then pnaic="Asieor";
if (pn='85' | pn='74' | pn='75' | pn='78' | pn='79' | pn='82' | pn='84' | pn='80' ) then pnaic="Asiemo";
/* Maghreb */
if pn2=31 then pnaic="Maghreb";
if pn2=45 then pnaic="Maghreb";
if pn2=52 then pnaic="Maghreb";
if immig=1 & missing(pnaic) then pnaic="Other";
if immig=0 then pnaic="Natif";
/* pays harmonis�s PNAID */
if pn='01'	 then pnaid="allemagne";
if pn='02'	 then pnaid="autriche";
if pn='03'	 then pnaid="belgique";
if pn='04'	 then pnaid="bulgarie";
if pn='05'	 then pnaid="danemark";
if pn='06'	 then pnaid="espagne";
if pn='07'	 then pnaid="finlande";
if pn='08'	 then pnaid="grece";
if pn='09'	 then pnaid="hongrie";
if pn='10'	 then pnaid="irlande";
if pn='11'	 then pnaid="italie";
if pn='12'	 then pnaid="luxembourg";
if pn='13'	 then pnaid="norvege";
if pn='14'	 then pnaid="pays-bas";
if pn='15'	 then pnaid="pologne";
if pn='16'	 then pnaid="portugal";
if pn='17'	 then pnaid="roumanie";
if pn='18'	 then pnaid="uk";
if pn='19'	 then pnaid="suede";
if pn='20'	 then pnaid="suisse";
if pn='21'	 then pnaid="tchecoslovaquie";
if pn='22'	 then pnaid="yougoslavie";
if pn='31' then pnaid="algerie";
if pn='33'	 then pnaid="cameroun";
if pn='34'	 then pnaid="centrafrique";
if pn='35'	 then pnaid="congo";
if pn='36'	 then pnaid="cotedivoire";
if pn='37'	 then pnaid="dahomey";
if pn='39'	 then pnaid="gabon";
if pn='40'	 then pnaid="guinee";
if pn='42'	 then pnaid="haute-volta";
if pn='43'	 then pnaid="madagascar";
if pn='44'	 then pnaid="mali";
if pn='45' then pnaid="maroc";
if pn='46'	 then pnaid="mauritanie";
if pn='47'	 then pnaid="niger";
if pn='48'	 then pnaid="egypte";
if pn='49'	 then pnaid="senegal";
if pn='50'	 then pnaid="tchad";
if pn='51'	 then pnaid="togo";
if pn='52' then pnaid="tunisie";
if pn='71'	 then pnaid="cambodge";
if pn='72'	 then pnaid="chine";
if pn='73'	 then pnaid="inde";
if pn='74'	 then pnaid="iran";
if pn='75'	 then pnaid="israel";
if pn='76'	 then pnaid="japon";
if pn='77'	 then pnaid="laos";
if pn='78'	 then pnaid="liban";
if pn='79'	 then pnaid="syrie";
if pn='80'	 then pnaid="turquie";
if pn='81'	 then pnaid="vietnam";
if pn='85'	 then pnaid="urss";
/*******************/
if missing(pnaid) then pnaid=pnaic;
if immig=0 then pnaid="Natif";

/* Education : DIP */
if DIP='01' or DIP='02' then educ='CEPP';
if DIP='10' or DIP='21' or DIP='22' then educ='BEPC';
if DIP='31' or DIP='32' or DIP='33' or DIP='34' then educ='NBAC';
if DIP='41' or DIP='42' or DIP='43' or DIP='44' or DIP='51' or DIP='52' then educ='BACP';

/* LF statut */
if TA=1 then emploi=1;
else emploi=0;
if TA=1 or TA=3 then actif=1;
else actif=0;
if TA=5 then etud=1;
else etud=0;
if TA=6 then milit=1;
else milit=0;
if TA=3 then chom=1;
else chom=0;
drop ta;
/* CSP */
if (cs8='3') then csp="cad" ;
if (cs8='5') then csp="emp" ;
if (cs8='6') then csp="ouv" ;

/* industrial classification *
if not(ae100='**') then ae100b=input(ae100,best4.);
/* Industrie *
if not(missing(ae100b)) then indus=(AE100b<=56);
*/

indus=(ae15='02' | ae15='03' | ae15='04' | ae15='05' | ae15='06');

/* profession d�taill�e */
prof = p;

/* surpeuplement */
/* mod�r� & accentu� */
surpm=(ip='4' | ip='5');
/* accentu� */
surp=(ip='5');

/* movers */
movers=1-(ira='1');

/* construction date CONSTD */
if aac1="1" then constd="0b1871";
if aac1="2" then constd="1871-1914";
if aac1="3" then constd="1915-1948";
if aac1="4" then constd="1949-1961";
if aac1="5" then constd="1962-1967";
if aac1="6" then constd="1968-1974";
if aac1="7" then constd="1972-1982";

/* age of the building BUILDAG */
if aac1="1" then buildag=111;
if aac1="2" then buildag=90;
if aac1="3" then buildag=51;
if aac1="4" then buildag=27;
if aac1="5" then buildag=18;
if aac1="6" then buildag=11;
if aac1="7" then buildag=4;

/*  nombre d'�tages */
nbeta=etm;
run;

/* statdes */

%corrUU99(1982);


%hcent(1982);

data sample;
length iris $16;
set sample;
/* d�finitions g�ographiques */
ville=uu99;
iris=ville!!nquartier;
run;

Data harmo.hrp82;
set sample (keep = cgeo aac1 age actif chom coup coupch elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof surp surpm movers
constd buildag nbeta);
run;

Data hmini.hrp82;
set sample (keep = cgeo aac1 age actif chom coup coupch elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof surp surpm movers
constd buildag nbeta);
/*******************/
y=ranbin(3,1,0.01);
if y=0 then delete;
drop y;
sond=sond*100;
/******************/
run;

/********/
/* 1990 */
/********/
data sample;
length depcom $5;
set tbrp.rp90;
depcom=cats(d,c);
cgeo=depcom;
/* sond */
newsond = input(sond,4.0); 
drop sond; 
rename newsond=sond; 
/* CIL harmonise avec table des corresp 
if missing(cil) then cil='   '; */
run;

data sample;
length ilot $14;
length quartier $14;
set sample;
/*************************/
ilot=d||c||cil||il;
/*************************/
quartier=cats(d,c,cil);
tract=quartier;
run;
/* merge avec iris 90 */
proc sort data=sample;
by ilot;
run;
/* iris 90 */
data ilocor;
set ilo9099.i90ir_f (keep = dccilil comp9099 dcomiris);
rename dccilil= ilot;
run;
proc sort data=ilocor;
by ilot;
run;
data sample;
merge sample (in = x) ilocor;
by ilot;
if x;
run;

data sample;
set sample;

length pnaic $7;
length pnaid $15;
length constd $9;

/* hlm */
if stocd='22' then hlm=1;
else hlm=0;
drop stocd;

/* sexe */
if sexe=1 then homme=1;
else homme=0;
drop sexe;

/* age */
newage = input(age,3.0); 
drop age; 
rename newage=age; 

/* couple */
if moco='21' or moco='22' then coup=1;
else coup=0;
/* couple with children */
if moco='22' then coupch=1;
else coupch=0;
/* famille monoparentale */
monop=(moco='23');
/* Head of Household */
hhd=(lprm='1');

/* immigrant status */
if  NAT1='11' then immig=0;
else immig=1;
drop nat1;
/* not born in Fce */
if not(INAI='4') then immig=0;
drop inai;
/* living abroad in 1990 */
if cmig=7 then abd=1;
else abd=0;
drop cmig;
/* new immigrants */
if immig=1 and abd=1 then nimmig=1;
else nimmig=0;

/* europe & neuropee */
europe=(pnai='007' or pnai='008' or pnai='020' or pnai='040' or 
pnai='056' or pnai='100' or pnai='196' or pnai='200' or 
pnai='208' or pnai='234' or pnai='238' or pnai='246' or 
pnai='278' or pnai='279' or pnai='280' or pnai='292' or 
pnai='300' or pnai='304' or pnai='308' or pnai='336' or 
pnai='348' or pnai='352' or pnai='372' or pnai='438' or 
pnai='442' or pnai='470' or pnai='492' or pnai='528' or 
pnai='574' or pnai='578' or pnai='642' or pnai='654' or 
pnai='674' or pnai='744' or pnai='752' or pnai='756' or 
pnai='826' or pnai='890' or pnai='616' or pnai='620' or pnai='724'
or pnai='380')*immig;
neurope=immig*(1-europe);

/* r�gions harmonis�es PNAIC */
if europe=1 then pnaic="Europe";
if (pnai='012' or pnai='504' or pnai='788') & immig=1 then pnaic="Maghreb";
if (pnai='002' or pnai='072' or 
pnai='108' or pnai='120' or pnai='132' or pnai='140' or 
pnai='148' or pnai='174' or pnai='178' or pnai='204' or 
pnai='226' or pnai='230' or pnai='262' or pnai='266' or 
pnai='270' or pnai='272' or pnai='288' or pnai='324' or 
pnai='384' or pnai='404' or pnai='426' or pnai='430' or 
pnai='434' or pnai='450' or pnai='454' or pnai='466' or 
pnai='478' or pnai='508' or pnai='516' or pnai='562' or 
pnai='566' or pnai='624' or pnai='646' or pnai='678' or 
pnai='686' or pnai='690' or pnai='694' or pnai='706' or 
pnai='710' or pnai='716' or pnai='732' or pnai='736' or 
pnai='748' or pnai='768' or pnai='800' or pnai='818' or 
pnai='834' or pnai='854' or pnai='894' or pnai='024'  or pnai='180'
) & immig=1 then pnaic="Africa";
if (pnai='116' or pnai='004' or pnai='006' or
pnai='048' or pnai='050' or pnai='064' or pnai='364' or
pnai='096' or pnai='104' or pnai='156' or pnai='158' or
pnai='162' or pnai='166' or pnai='168' or pnai='274' or
pnai='334' or pnai='344' or pnai='356' or pnai='360' or
pnai='368' or pnai='376' or pnai='392' or pnai='400' or
pnai='408' or pnai='409' or pnai='410' or pnai='414' or
pnai='446' or pnai='458' or pnai='462' or pnai='480' or
pnai='496' or pnai='512' or pnai='524' or pnai='586' or
pnai='608' or pnai='626' or pnai='634' or pnai='682' or
pnai='702' or pnai='720' or pnai='760' or pnai='764' or
pnai='784' or pnai='810' or pnai='886' or pnai='144' or
pnai='792' or pnai='422' or pnai='704' or pnai='418') & immig=1 then pnaic="Asiemo";
/********/
if (pnai='116' or pnai='392' or pnai='418' or
pnai='586' or pnai='704' or pnai='608' or
pnai='156' or pnai='144' or pnai='408' or
pnai='409' or pnai='410' or pnai='764' or
pnai='356' or pnai='360')then pnaic="Asieor";
if immig=0 then pnaic="Natif";
if immig=1 & missing(pnaic) then pnaic="Other";

/* pays harmonis�s PNAID */
if pnai='012' then pnaid="algerie";
if pnai='278' or pnai='279' or pnai='280'	then 	pnaid='allemagne'	;
if pnai='040'	then 	pnaid='autriche'	;
if pnai='056'	then 	pnaid='belgique'	;
if pnai='100'	then 	pnaid='bulgarie'	;
if pnai='208'	then 	pnaid='danemark'	;
if pnai='724'	then 	pnaid='espagne'	;
if pnai='246'	then 	pnaid='finlande'	;
if pnai='300'	then 	pnaid='grece'	;
if pnai='348'	then 	pnaid='hongrie'	;
if pnai='372'	then 	pnaid='irlande'	;
if pnai='380'	then 	pnaid='italie'	;
if pnai='442'	then 	pnaid='luxembourg'	;
if pnai='578'	then 	pnaid='norvege'	;
if pnai='528'	then 	pnaid='pays-bas'	;
if pnai='616'	then 	pnaid='pologne'	;
if pnai='620'	then 	pnaid='portugal'	;
if pnai='642'	then 	pnaid='roumanie'	;
if pnai='826'	then 	pnaid='uk'	;
if pnai='752'	then 	pnaid='suede'	;
if pnai='756'	then 	pnaid='suisse'	;
if pnai='200'	then 	pnaid='tchecoslovaquie'	;
if pnai='890'	then 	pnaid='yougoslavie'	;
if pnai='120'	then 	pnaid='cameroun'	;
if pnai='140'	then 	pnaid='centrafrique'	;
if pnai='178'	then 	pnaid='congo'	;
if pnai='384'	then 	pnaid='cotedivoire'	;
if pnai='204'	then 	pnaid='dahomey'	;
if pnai='266'	then 	pnaid='gabon'	;
if pnai='324'	then 	pnaid='guinee'	;
if pnai='854'	then 	pnaid='haute-volta'	;
if pnai='450'	then 	pnaid='madagascar'	;
if pnai='466'	then 	pnaid='mali'	;
if pnai='478'	then 	pnaid='mauritanie'	;
if pnai='562'	then 	pnaid='niger'	;
if pnai='818'	then 	pnaid='egypte'	;
if pnai='686'	then 	pnaid='senegal'	;
if pnai='148'	then 	pnaid='tchad'	;
if pnai='768'	then 	pnaid='togo'	;
if pnai='116'	then 	pnaid='cambodge'	;
if pnai='156' or pnai='158'	then 	pnaid='chine'	;
if pnai='356'	then 	pnaid='inde'	;
if pnai='364'	then 	pnaid='iran'	;
if pnai='376'	then 	pnaid='israel'	;
if pnai='392'	then 	pnaid='japon'	;
if pnai='418'	then 	pnaid='laos'	;
if pnai='422'	then 	pnaid='liban'	;
if pnai='504' then pnaid="maroc";
if pnai='760'	then 	pnaid='syrie'	;
if pnai='788' then pnaid="tunisie";
if pnai='792'	then 	pnaid='turquie'	;
if pnai='704'	then 	pnaid='vietnam'	;
if pnai='810'	then 	pnaid='urss'	;
if missing(pnaid) then pnaid=pnaic;
if immig=0 then pnaid="Natif";
if immig=1 and missing(pnaid) then pnaid=pnaic;

/* Education : DIP */
if DIPL1='2' or DIPL1='3' then educ='CEPP';
if DIPL1='4' or DIPL1='5' or DIPL1='6' then educ='BEPC';
if DIPL1='7' then educ='NBAC';
if DIPL1='8' or DIPL1='9' then educ='BACP';

/* LF statut */
if TACT=11 then emploi=1;
else emploi=0;
if TACT=11 or TACT=12 then actif=1;
else actif=0;
if TACT=22 then etud=1;
else etud=0;
if TACT=13 then milit=1;
else milit=0;
if TACT=12 then chom=1;
else chom=0;
drop tact;

/* CSP */
if (cs8='3') then csp="cad" ;
if (cs8='5') then csp="emp" ;
if (cs8='6') then csp="ouv" ;

/* industrial classification *
if not(ae100='**') then ae100b=input(ae100,best4.);
/* Industrie *
if not(missing(ae100b)) then indus=(AE100b<=56);*/
indus=(ae15='02' | ae15='03' | ae15='04' | ae15='05' | ae15='06');

/* movers */
movers=1-(iran='1');

/* construction date CONSTD */
if achi="1" then constd="0b1915";
if achi="2" then constd="1915-1948";
if achi="3" then constd="1949-1967";
if achi="4" then constd="1968-1974";
if achi="5" then constd="1975-1981";
if achi="6" then constd="1982-1990";
if achi="7" then constd="1990";

/* age of the building BUILDAG */
if achi="1" then buildag=90;
if achi="2" then buildag=59;
if achi="3" then buildag=32;
if achi="4" then buildag=19;
if achi="5" then buildag=12;
if achi="6" then buildag=4;
if achi="7" then buildag=1;
run;

%corrUU99(1990);
%hcent(1990);

data sample;
length iris $16;
set sample;
/* d�finitions g�ographiques */
ville=uu99;
iris=ville!!nquartier;
if not(missing(dcomiris)) then iris=ville!!'I'!!dcomiris;
run;
proc freq data=sample noprint;
weight sond;
table iris / out=popiris2;
run;

data popiris2;
set popiris2;
drop percent;
rename count=popiris2;
run;

proc sort data=popiris2;
by iris;
run;
proc sort data=sample;
by iris;
run;
data sample;
merge sample (in =x) popiris2;
by iris;
if x;
rename popiris= popiriso;
rename popiris2= popiris;
run;
proc means data=sample;
var popiriso popiris;
run;

Data harmo.hrp90;
set sample (keep = cgeo age actif chom coup coupch elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof movers
constd buildag );
run;

Data hmini.hrp90;
set sample (keep = cgeo age actif chom coup coupch elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof movers
constd buildag );
/*******************/
y=ranbin(3,1,0.01);
if y=0 then delete;
drop y;
sond=sond*100;
/******************/
run;

/********/
/* 1999 */
/********/
data sample;
length depcom $5;
set tbrp.rp99;
depcom=cats(d,c);
cgeo=depcom;
/* observation = humain being
if sex is missing then delete */
if missing(sexe) then delete;
run;

data sample;
set sample;

length pnaic $7;
length pnaid $15;
length anmen $4;
length constd $9;

/* hlm */
hlm=(stocd='22');
drop stocd;



/* sexe */
if sexe=1 then homme=1;
else homme=0;
drop sexe;

/* age */
agenew=input(age, 8.0);
drop age;
rename agenew=age;

/* couple */
if moco='21' or moco='22' then coup=1;
else coup=0;
/* couple with children */
if moco='22' then coupch=1;
else coupch=0;
/* famille monoparentale */
monop=(moco='23');
/* Head of Household */
hhd=(lprm='1');

/* immigrant status */
/*immigres*/
if inat1='12' | inat1='21' | inat1='22'  then immig=1;
else immig=0;
/* not born in france*/
if inai2='1' | inai2='2' then immig=0;

/* living abroad in 1990 */
if iran=7 or iran=8 then abd=1;
else abd=0;
drop iran;
/* new immigrants */
if immig=1 and abd=1 then nimmig=1;
else nimmig=0;

/* europe & neurope */
pn2=pnai3*1;
europe=( pn2=>11 and pn2<=15 )*immig;
neurope=immig*(1-europe);

/* r�gions harmonis�es PNAIC */
if (pn2=>11 and pn2<=15 ) & immig=1 then pnaic="Europe";
if (pn2>=21 and pn2<=23) & immig=1 then pnaic="Maghreb";
if (pn2=24) & immig=1 then pnaic="Africa";
if (pn2>=31 and pn2<=35) & immig=1 then pnaic="Asieor";
if (pnai1="31A" or pnai1="31B" or pnai1="31C" or pnai1="33A" or 
pnai1="33B" or pnai1="33C" or pnai1="33D" or pnai1="33E" or 
pnai1="33I" or pnai1="33K" or pnai1="33L" or pnai1="33M" or 
pnai1="33O") & immig=1 then pnaic="Asieor";
if (pnai1="33F" or pnai1="33G" or pnai1="33H" or pnai1="33N" or
pnai1="33J" or pnai1="33P" or pnai1="33Q" or pnai1="32F" or
pnai1="32G" or pnai1="32H" or pnai1="32A" or pnai1="32B" or
pnai1="32C" or pnai1="32D" or pnai1="32E") & immig=1 then pnaic="Asiemo";
if immig=0 then pnaic="Natif";
if immig=1 & missing(pnaic) then pnaic="Other";

/* pays harmonis�s PNAID */
if pnai='276' then 	pnaid='allemagne'	;
if pnai='040'	then 	pnaid='autriche'	;
if pnai='056'	then 	pnaid='belgique'	;
if pnai='100'	then 	pnaid='bulgarie'	;
if pnai='208'	then 	pnaid='danemark'	;
if pnai='724'	then 	pnaid='espagne'	;
if pnai='246'	then 	pnaid='finlande'	;
if pnai='300'	then 	pnaid='grece'	;
if pnai='348'	then 	pnaid='hongrie'	;
if pnai='372'	then 	pnaid='irlande'	;
if pnai='380'	then 	pnaid='italie'	;
if pnai='442'	then 	pnaid='luxembourg'	;
if pnai='578'	then 	pnaid='norvege'	;
if pnai='528'	then 	pnaid='pays-bas'	;
if pnai='616'	then 	pnaid='pologne'	;
if pnai='620'	then 	pnaid='portugal'	;
if pnai='642'	then 	pnaid='roumanie'	;
if pnai='826'	then 	pnaid='uk'	;
if pnai='752'	then 	pnaid='suede'	;
if pnai='756'	then 	pnaid='suisse'	;
if pnai='200'	then 	pnaid='tchecoslovaquie'	;
if pnai='890'	then 	pnaid='yougoslavie'	;
if pnai='120'	then 	pnaid='cameroun'	;
if pnai='140'	then 	pnaid='centrafrique'	;
if pnai='178'	then 	pnaid='congo'	;
if pnai='384'	then 	pnaid='cotedivoire'	;
if pnai='204'	then 	pnaid='dahomey'	;
if pnai='266'	then 	pnaid='gabon'	;
if pnai='324'	then 	pnaid='guinee'	;
if pnai='854'	then 	pnaid='haute-volta'	;
if pnai='450'	then 	pnaid='madagascar'	;
if pnai='466'	then 	pnaid='mali'	;
if pnai='478'	then 	pnaid='mauritanie'	;
if pnai='562'	then 	pnaid='niger'	;
if pnai='818'	then 	pnaid='egypte'	;
if pnai='686'	then 	pnaid='senegal'	;
if pnai='148'	then 	pnaid='tchad'	;
if pnai='768'	then 	pnaid='togo'	;
if pnai='116'	then 	pnaid='cambodge'	;
if pnai='156' or pnai='158'	then 	pnaid='chine'	;
if pnai='356'	then 	pnaid='inde'	;
if pnai='364'	then 	pnaid='iran'	;
if pnai='376'	then 	pnaid='israel'	;
if pnai='392'	then 	pnaid='japon'	;
if pnai='418'	then 	pnaid='laos'	;
if pnai='422'	then 	pnaid='liban'	;
if pnai='760'	then 	pnaid='syrie'	;
if pnai='792'	then 	pnaid='turquie'	;
if pnai='704'	then 	pnaid='vietnam'	;
if pnai='643' | pnai='804' | pnaid="398"	then 	pnaid='urss'	;
if pnai='012' then pnaid="algerie";
if pnai='504' then pnaid="maroc";
if pnai='788' then pnaid="tunisie";
if immig=0 then pnaid="Natif";
if immig=1 and missing(pnaid) then pnaid=pnaic;

/* Education : DIP */
if dipl='0' or dipl='1' then educ='CEPP';
if dipl='2' or dipl='3' or dipl='4' then educ='BEPC';
if dipl='5' or dipl='6' then educ='NBAC';
if dipl='7' then educ='BACP';
if dipl='8' then educ='BACP';

/* LF statut */
emploi=(TACT=11);
if TACT=11 or TACT=12 then actif=1;
else actif=0;
if TACT=22 then etud=1;
else etud=0;
milit=(TACT=13);
chom=(TACT=12);

/* CSP */
if (cs8='3') then csp="cad" ;
if (cs8='5') then csp="emp" ;
if (cs8='6') then csp="ouv" ;
/****************************************/
/* temps pass�e en France: variable DUR */
/****************************************/
darriv=arriv*1+1000;
if not(missing(darriv)) then dur=1999-darriv;
/*****************************/

/* Industrie */
indus=(naf4='T');

/* ajuste les poids */
sond=sond*0.9;

/* movers */
movers=1-(iran='1');

/* ann�e d'am�nagement */
anmen=cats("19",aemm);
if aemm="XX" | aemm="" then anmen="";
/* tenure logement */
tenlog=1999-input(anmen,4.);

/* construction date CONSTD */
if achi="1" then constd="0b1915";
if achi="2" then constd="1915-1948";
if achi="3" then constd="1949-1967";
if achi="4" then constd="1968-1974";
if achi="5" then constd="1975-1981";
if achi="6" then constd="1982-1990";
if achi="7" then constd="1990-1999";
if achi="8" then constd="1999";

/* age of the building BUILDAG */
if achi="1" then buildag=84;
if achi="2" then buildag=68;
if achi="3" then buildag=41;
if achi="4" then buildag=28;
if achi="5" then buildag=21;
if achi="6" then buildag=14;
if achi="7" then buildag=5;
if achi="8" then buildag=1;
run;

%corrUU99(1999);

data sample;
length iris $22;
length gqt $20;
set sample;
/* d�finitions g�ographiques */
ville=uu99;
/* iris=ville!!d!!c!!cil!!iris2000;
gqt=ville!!d!!c!!cil!!gq;*/ 
iris=ville!!d!!c!!iris2000;
gqt=ville!!d!!c!!gq;
run;

%popiris;

Data harmo.hrp99;
set sample (keep = cgeo age actif chom coup coupch dur elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof achi aachi anmen tenlog
constd buildag nbeta movers);
run;

Data hmini.hrp99;
set sample (keep = cgeo age actif chom coup coupch dur elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp prof achi aachi anmen tenlog
constd buildag nbeta movers);
/*******************/
y=ranbin(3,1,0.01);
if y=0 then delete;
drop y;
sond=sond*100;
/******************/
run;

/********/
/* 2007 */
/********/
data sample;
set tbrp.rp07;
depcom= commune_LR ;
d=substr(commune_LR,1,2);
cgeo=depcom;
run;

Data sample;
set sample;

length pnaic $7;
length pnaid $15;
length constd $9;

/* hlm */
hlm=(stocd='22');
/* observation = humain being
if sex is missing then delete */
if missing(sexe) then delete;

/* sexe */
homme=(sexe='1');

/* age */
age = input(agerev,3.0); 

/* couple */
if moco='21' or moco='22' then coup=1;
else coup=0;
/* couple with children */
if moco='22' then coupch=1;
else coupch=0;
/* famille monoparentale */
monop=(moco='23');
/* Head of Household */
hhd=(lprm='1');

/* immigrant status */
immig=(immi="1");
drop immi;
if natc="0" then immig=0;

/* new immigrants arriv� apr�s 1999*/
if iran='8' or iran='9' or (arrivr='9') then abd=1;
else abd=0;
nimmig=((abd=1) & immig=1);

/* europe & neurope */
/* nationalit� d'origine */
/* d�taill�e */
natf=nat;
if nat="998" then natf=natn;
/* condens�e */
natcf=natc;
if natc=1 then natcf=natnc;

europe=(natcf="2" OR natcf="3")*immig;
neurope=(1-europe)*immig;

/* r�gions harmonis�es PNAIC */
if (natcf="2" OR natcf="3") & immig=1 then pnaic="Europe";
if (natcf='4') & immig=1 then pnaic="Africa";
if (natf='012' OR natf='504' OR natf='788') then pnaic="Maghreb";
if (natf="116" or natf="418" or natf="704" or natf="156" or
natf="408" or natf="410" or natf="356" or natf="360" or
natf="392" or natf="586" or natf="608" or natf="144" or
natf="764") & immig=1 then pnaic="Asieor";
if (natf="368" or natf="364" or natf="376" or natf="760" or
natf="422" or natf="792" or natf="860" or natf="762" or
natf="795" or natf="051" or natf="031" or natf="268" or
natf="398" or natf="417") & immig=1  then pnaic="Asiemo";
if immig=0 then pnaic="Natif";
if immig=1 & missing(pnaic) then pnaic="Other";

/* pays harmonis�s PNAID */
if natf='276' then 	pnaid='allemagne'	;
if natf='040'	then 	pnaid='autriche'	;
if natf='056'	then 	pnaid='belgique'	;
if natf='100'	then 	pnaid='bulgarie'	;
if natf='208'	then 	pnaid='danemark'	;
if natf='724'	then 	pnaid='espagne'	;
if natf='246'	then 	pnaid='finlande'	;
if natf='300'	then 	pnaid='grece'	;
if natf='348'	then 	pnaid='hongrie'	;
if natf='372'	then 	pnaid='irlande'	;
if natf='380'	then 	pnaid='italie'	;
if natf='442'	then 	pnaid='luxembourg'	;
if natf='578'	then 	pnaid='norvege'	;
if natf='528'	then 	pnaid='pays-bas'	;
if natf='616'	then 	pnaid='pologne'	;
if natf='620'	then 	pnaid='portugal'	;
if natf='642'	then 	pnaid='roumanie'	;
if natf='826'	then 	pnaid='uk'	;
if natf='752'	then 	pnaid='suede'	;
if natf='756'	then 	pnaid='suisse'	;
if natf='200'  | natf='703' | natf='203'	then 	pnaid='tchecoslovaquie'	;
if natf='890' | natf='891'	then 	pnaid='yougoslavie'	;
if natf='120'	then 	pnaid='cameroun'	;
if natf='140'	then 	pnaid='centrafrique'	;
if natf='178'	then 	pnaid='congo'	;
if natf='384'	then 	pnaid='cotedivoire'	;
if natf='204'	then 	pnaid='dahomey'	;
if natf='266'	then 	pnaid='gabon'	;
if natf='324'	then 	pnaid='guinee'	;
if natf='854'	then 	pnaid='haute-volta'	;
if natf='450'	then 	pnaid='madagascar'	;
if natf='466'	then 	pnaid='mali'	;
if natf='478'	then 	pnaid='mauritanie'	;
if natf='562'	then 	pnaid='niger'	;
if natf='818'	then 	pnaid='egypte'	;
if natf='686'	then 	pnaid='senegal'	;
if natf='148'	then 	pnaid='tchad'	;
if natf='768'	then 	pnaid='togo'	;
if natf='116'	then 	pnaid='cambodge'	;
if natf='156' or natf='158'	then 	pnaid='chine'	;
if natf='356'	then 	pnaid='inde'	;
if natf='364'	then 	pnaid='iran'	;
if natf='376'	then 	pnaid='israel'	;
if natf='392'	then 	pnaid='japon'	;
if natf='418'	then 	pnaid='laos'	;
if natf='422'	then 	pnaid='liban'	;
if natf='760'	then 	pnaid='syrie'	;
if natf='792'	then 	pnaid='turquie'	;
if natf='704'	then 	pnaid='vietnam'	;
if natf='643' | natf='804' | natf="398"	then 	pnaid='urss'	;
if natf='012' then pnaid="algerie";
if natf='504' then pnaid="maroc";
if natf='788' then pnaid="tunisie";
if immig=0 then pnaid="Natif";
if immig=1 and missing(pnaid) then pnaid=pnaic;

/* Education : DIP */
if dipl='01' or dipl='02' or dipl='03' or dipl='11' then educ='CEPP';
if dipl='12' or dipl='13' or dipl='14' then educ='BEPC';
if dipl='15' or dipl='16' then educ='NBAC';
if dipl='17' then educ='BACP';
if dipl='18' then educ='BACP';

/* LF statut */
/* chomage */
chom=(tact='12');
/* actif */
actif=(tact='11' | tact='12');
etud=(tact='22' | tact='23');
milit=(cs3='83');

/* CSP */
if (cs2='3') then csp="cad" ;
if (cs2='5') then csp="emp" ;
if (cs2='6') then csp="ouv" ;

/****************************************/
/* temps pass�e en France: variable DUR */
/****************************************/
ccol=c_annee_col*1;
if not(arriv="ZZZZ") then darriv=arriv;
darriv=darriv*1;
if not(darriv=0) then dur=ccol-darriv;
/*****************************/

/* Industrie */
indus=(nafs08='B' | nafs08='C' | nafs08='D' | nafs08='E');

/* movers */
movers=1-(iran='1');

/* ann�e d'am�nagement */
anmen=aemm;
if aemm="0000" | aemm="ZZZZ" then anmen="";
/* tenure logement */
tenlog=input(c_annee_col,4.)-input(anmen,4.);

/* construction date CONSTD */
if achlr="1" then constd="0b1949";
if achlr="2" then constd="1949-1974";
if achlr="3" then constd="1975-1981";
if achlr="4" then constd="1982-1989";
if achlr="5" then constd="1990-1998";
if achlr="6" then constd="1999-2003";
if achlr="7" then constd="2007";
if achlr="9" then constd="2007";

/* age of the building BUILDAG */
if achlr="1" then buildag=58;
if achlr="2" then buildag=46;
if achlr="3" then buildag=29;
if achlr="4" then buildag=22;
if achlr="5" then buildag=13;
if achlr="6" then buildag=6;
if achlr="7" then buildag=2;
if achlr="9" then buildag=1;
run;

%corrUU99(2007);


data sample;
length iris $16;
length gqt $20;
set sample;
/* d�finitions g�ographiques */
ville=uu99;
iris=ville||trim(iris08_LR);
gqt=ville||trim(dept_lr)||trim(commune_lr)||trim(gq_lr);
run;

%popiris;

Data harmo.hrp07;
set sample (keep = c_annee_col cgeo age actif chom coup coupch dur elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp gqt prof achl movers anmen 
tenlog
constd buildag );
run;

Data hmini.hrp07;
set sample (keep = c_annee_col cgeo age actif chom coup coupch dur elim educ etud 
europe hhd hlm homme immig indus iris milit monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp gqt prof achl movers anmen
tenlog 
constd buildag );
/*******************/
y=ranbin(3,1,0.01);
if y=0 then delete;
drop y;
sond=sond*100;
/******************/
run;

/********/
/* 2012 */
/********/
data sample;
set tbrp.rp12;
depcom= commune_resid ;
d=substr(commune_resid,1,2);
cgeo=depcom;
run;

Data sample;
set sample;

length pnaic $7;
length pnaid $15;
length constd $9;

/* hlm */
hlm=(stocd='22');
/* observation = humain being
if sex is missing then delete */
if missing(sexe) then delete;

/* sexe */
homme=(sexe='1');

/* age */
age = input(agerev,3.0); 

/* couple */
if moco='21' or moco='22' then coup=1;
else coup=0;
/* couple with children */
if moco='22' then coupch=1;
else coupch=0;
/* famille monoparentale */
monop=(moco='23');
/* Head of Household */
hhd=(lprm='1');

/* immigrant status */
immig=(immi="1");
drop immi;
if natc="0" then immig=0;

/* new immigrants arriv� apr�s 1999*/
if iran='8' or iran='9' or (arrivr='9') then abd=1;
else abd=0;
nimmig=((abd=1) & immig=1);

/* europe & neurope */
/* nationalit� d'origine */
/* d�taill�e */
natf=nat;
if nat="998" then natf=natn;
/* condens�e */
natcf=natc;
if natc=1 then natcf=natnc;

europe=(natcf="2" OR natcf="3")*immig;
neurope=(1-europe)*immig;

/* r�gions harmonis�es PNAIC */
if (natcf="2" OR natcf="3") & immig=1 then pnaic="Europe";
if (natcf='4') & immig=1 then pnaic="Africa";
if (natf='012' OR natf='504' OR natf='788') then pnaic="Maghreb";
if (natf="116" or natf="418" or natf="704" or natf="156" or
natf="408" or natf="410" or natf="356" or natf="360" or
natf="392" or natf="586" or natf="608" or natf="144" or
natf="764") & immig=1 then pnaic="Asieor";
if (natf="368" or natf="364" or natf="376" or natf="760" or
natf="422" or natf="792" or natf="860" or natf="762" or
natf="795" or natf="051" or natf="031" or natf="268" or
natf="398" or natf="417") & immig=1  then pnaic="Asiemo";
if immig=0 then pnaic="Natif";
if immig=1 & missing(pnaic) then pnaic="Other";

/* pays harmonis�s PNAID */
if natf='276' then 	pnaid='allemagne'	;
if natf='040'	then 	pnaid='autriche'	;
if natf='056'	then 	pnaid='belgique'	;
if natf='100'	then 	pnaid='bulgarie'	;
if natf='208'	then 	pnaid='danemark'	;
if natf='724'	then 	pnaid='espagne'	;
if natf='246'	then 	pnaid='finlande'	;
if natf='300'	then 	pnaid='grece'	;
if natf='348'	then 	pnaid='hongrie'	;
if natf='372'	then 	pnaid='irlande'	;
if natf='380'	then 	pnaid='italie'	;
if natf='442'	then 	pnaid='luxembourg'	;
if natf='578'	then 	pnaid='norvege'	;
if natf='528'	then 	pnaid='pays-bas'	;
if natf='616'	then 	pnaid='pologne'	;
if natf='620'	then 	pnaid='portugal'	;
if natf='642'	then 	pnaid='roumanie'	;
if natf='826'	then 	pnaid='uk'	;
if natf='752'	then 	pnaid='suede'	;
if natf='756'	then 	pnaid='suisse'	;
if natf='200'  | natf='703' | natf='203'	then 	pnaid='tchecoslovaquie'	;
if natf='890' | natf='891'	then 	pnaid='yougoslavie'	;
if natf='120'	then 	pnaid='cameroun'	;
if natf='140'	then 	pnaid='centrafrique'	;
if natf='178'	then 	pnaid='congo'	;
if natf='384'	then 	pnaid='cotedivoire'	;
if natf='204'	then 	pnaid='dahomey'	;
if natf='266'	then 	pnaid='gabon'	;
if natf='324'	then 	pnaid='guinee'	;
if natf='854'	then 	pnaid='haute-volta'	;
if natf='450'	then 	pnaid='madagascar'	;
if natf='466'	then 	pnaid='mali'	;
if natf='478'	then 	pnaid='mauritanie'	;
if natf='562'	then 	pnaid='niger'	;
if natf='818'	then 	pnaid='egypte'	;
if natf='686'	then 	pnaid='senegal'	;
if natf='148'	then 	pnaid='tchad'	;
if natf='768'	then 	pnaid='togo'	;
if natf='116'	then 	pnaid='cambodge'	;
if natf='156' or natf='158'	then 	pnaid='chine'	;
if natf='356'	then 	pnaid='inde'	;
if natf='364'	then 	pnaid='iran'	;
if natf='376'	then 	pnaid='israel'	;
if natf='392'	then 	pnaid='japon'	;
if natf='418'	then 	pnaid='laos'	;
if natf='422'	then 	pnaid='liban'	;
if natf='760'	then 	pnaid='syrie'	;
if natf='792'	then 	pnaid='turquie'	;
if natf='704'	then 	pnaid='vietnam'	;
if natf='643' | natf='804' | natf="398"	then 	pnaid='urss'	;
if natf='012' then pnaid="algerie";
if natf='504' then pnaid="maroc";
if natf='788' then pnaid="tunisie";
if immig=0 then pnaid="Natif";
if immig=1 and missing(pnaid) then pnaid=pnaic;

/* Education : DIP */
if dipl='01' or dipl='02' or dipl='03' or dipl='11' then educ='CEPP';
if dipl='12' or dipl='13' or dipl='14' then educ='BEPC';
if dipl='15' or dipl='16' then educ='NBAC';
if dipl='17' then educ='BACP';
if dipl='18' then educ='BACP';

/* LF statut */
/* chomage */
chom=(tact='12');
/* actif */
actif=(tact='11' | tact='12');
etud=(tact='22' | tact='23');

/* CSP */
cs21d=substr(cs2,1,1);
if (cs21d='3') then csp="cad" ;
if (cs21d='5') then csp="emp" ;
if (cs21d='6') then csp="ouv" ;

/****************************************/
/* temps pass�e en France: variable DUR */
/****************************************/
ccol=c_annee_col*1;
if not(arriv="ZZZZ") then darriv=arriv;
darriv=darriv*1;
if not(darriv=0) then dur=ccol-darriv;
/*****************************/

/* Industrie */
indus=(nafs08='B' | nafs08='C' | nafs08='D' | nafs08='E');

/* movers */
movers=1-(iran='1');

/* ann�e d'am�nagement */
anmen=aemm;
if aemm="0000" | aemm="ZZZZ" then anmen="";
/* tenure logement */
tenlog=input(c_annee_col,4.)-input(anmen,4.);

/* construction date CONSTD */
if achl="111" then constd="0b1946";
if achl="211" then constd="1946-1990";
if achl="311" then constd="1991-2005";
if achl="312" then constd="2006-2009";
if achl="313" then constd="2006-2009";
if achl="314" then constd="2006-2009";
if achl="315" then constd="2006-2009";
if achl="322" then constd="2009-2014";
if achl="323" then constd="2009-2014";
if achl="324" then constd="2009-2014";
if achl="325" then constd="2009-2014";
if achl="326" then constd="2009-2014";

/* age of the building BUILDAG */
if achl="111" then buildag=66;
if achl="211" then buildag=43;
if achl="311" then buildag=14;
if achl="312" then buildag=5;
if achl="313" then buildag=5;
if achl="314" then buildag=5;
if achl="315" then buildag=5;
if achl="322" then buildag=1;
if achl="323" then buildag=1;
if achl="324" then buildag=1;
if achl="325" then buildag=1;
if achl="326" then buildag=1;

run;

%corrUU99(2012);

data sample;
length iris $16;
length gqt $20;
set sample;
/* d�finitions g�ographiques */
ville=uu99;
iris=ville||trim(iris08_resid);
gqt=ville||trim(dept_resid)||trim(commune_resid)||trim(gq_resid);
run;

%popiris;

Data harmo.hrp12;
set sample (keep = c_annee_col cgeo age actif chom coup coupch dur elim educ etud 
europe hhd hlm homme immig indus iris  monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp gqt prof achl movers anmen
tenlog constd buildag );
run;

Data hmini.hrp12;
set sample (keep = c_annee_col cgeo age actif chom coup coupch dur elim educ etud 
europe hhd hlm homme immig indus iris  monop muu99 neurope 
nimmig pnaic pnaid popiris sond tu99 uu99 ville moco csp gqt prof achl movers anmen
tenlog constd buildag );
/*******************/
y=ranbin(3,1,0.01);
if y=0 then delete;
drop y;
sond=sond*100;
/******************/
run;

/* Sortie */
libname harmo "P:\Donnees\RP_Harmo\"; /* taille longue */
libname hmini "P:\Donnees\RP_Harmo\mini"; /* petite taille */
/* exporte rp07 pour �tudier le sorting 
proc export data=harmo.hrp07 outfile="P:\Donnees\RP_harmo\hrp07.dta" replace;
run;
proc export data=hmini.hrp07 outfile="P:\Donnees\RP_harmo\mini\hrp07.dta" replace;
run;
*/
/* exporte rp 82 */
proc export data=harmo.hrp82 outfile="P:\Donnees\RP_harmo\hrp82.dta" replace;
run;
proc export data=hmini.hrp82 outfile="P:\Donnees\RP_harmo\mini\hrp82.dta" replace;
run;

proc export data=harmo.hrp12 outfile="P:\Donnees\RP_harmo\hrp12.dta" replace;
run;
proc export data=hmini.hrp12 outfile="P:\Donnees\RP_harmo\mini\hrp12.dta" replace;
run;
